package sketch.shape.geom;

import java.awt.Graphics2D;

import sketch.shape.Shape;
import sketch.shape.geom.prim.Point;
import sketch.shape.geom.prim.gVector;

public class Parallelogram extends Shape {

	private Point refPoint; // top-left point

	private double angle; // radian from h-line to v-line

	private double width;

	private double height; // NOT vertical height
	                       // but v-line length

	public Parallelogram() {

	}

	public Point getCenter() {
		return new Point(getRefX() + getDX() * .5, getRefY() + getDY() * 5);
	}

	public double getAngle() {
		return angle;
	}

	public void setAngle(double angle) {
		this.angle = angle;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	public Point getRefPoint() {
		return refPoint;
	}

	public double getRefX() {
		return getRefPoint().getX();
	}

	public double getRefY() {
		return getRefPoint().getY();
	}

	public void setRefPoint(Point ref) {
		this.refPoint = ref.clone();
	}

	/*
	 * p1 to p4 takes the order of ulft - urt - lrt - llft
	 */
	public Point getP1() {
		return new Point(getRefPoint());
	}

	public Point getP2() {
		return getRefPoint().add(width, 0);
	}

	public Point getP3() {
		return getRefPoint().add(width - getDX(), getAbsoluteHeight());
	}

	public Point getP4() {
		return getRefPoint().add(-getDX(), getAbsoluteHeight());
	}

	public double getAbsoluteHeight() {
		return getHeight() * Math.sin(getAngle());
	}

	public double getDX() { // x dif
		return getHeight() * Math.cos(getAngle());
	}

	public double getDY() { // vertical height
		return getHeight() * Math.sin(getAngle());
	}

	public double getWidth() {
		return width;
	}

	public void setWidth(double width) {
		this.width = width;
	}

	public void drawComponent(Graphics2D g2) {
		super.drawComponent(g2);

		int height = (int) (getHeight() * Math.sin(getAngle()));
		int dx = (int) (getHeight() * Math.cos(getAngle()));
//System.out.println(height + ":" + dx);
		g2.draw(new java.awt.Polygon(
				new int[] {getRefPoint().intX(),
						   getRefPoint().intX() + (int)getWidth(),
						   getRefPoint().intX() + (int)getWidth() - dx,
						   getRefPoint().intX() - dx},
				new int[] {getRefPoint().intY(),
						   getRefPoint().intY(),
						   getRefPoint().intY() + height,
						   getRefPoint().intY() + height},
				4
		));
	}

	public java.awt.Shape getBound(double GAP) {
		GAP *= 2;
		gVector v = getP4().subtract(getP1()).dir().unitized();

		double an = getAngle() / 2;
//		System.out.println(v + ":" + an);
		Point p1 = getP1().add(v.rotate(-an - 0.5 * Math.PI).multiply(GAP));
		Point p2 = getP2().add(v.rotate(Math.PI - an).multiply(GAP));
		Point p3 = getP3().add(v.rotate(-an + 0.5 * Math.PI).multiply(GAP));
		Point p4 = getP4().add(v.rotate(-an).multiply(GAP));

		return new java.awt.Polygon(
				new int[] {p1.intX(), p2.intX(), p3.intX(), p4.intX()},
				new int[] {p1.intY(), p2.intY(), p3.intY(), p4.intY()},
				4
		);
	}

	public String[][] getFieldValues() {
		return new String[][] {
				{ "RefPoint", getRefPoint().toString() },
				{ "Angle", Double.toString(getAngle()) },
				{ "Width", Double.toString(getWidth()) },
				{ "Height", Double.toString(getHeight()) }
		};
	}
}
